using Cirrious.MvvmCross.ViewModels;
using System;
using TeaLightV5.Foundation.BL;
using TeaLightV5.Foundation.BL.Sync;
using TeaLightV5.Foundation.Dal;
using TeaLightV5.Foundation.MvvmCross;
using TeaLightV5.Foundation.MvvmCross.ViewModels;
using TeaLightV5.Foundation.Sal.Interfaces;

namespace TeaLightV5.Foundation.UX.ViewModels
{
    public abstract class EntityViewModel : TeaViewModel
    {
        public abstract void ReloadModel();
    }

    public abstract class EntityViewModel<T, TKey>
        : EntityViewModel
        where T : IEntity<TKey>, new()
         
    {
        protected EntityViewModel()
        {
            BaseSyncContext.UpdateFinished += SyncContextUpdateFinished;
        }

        void SyncContextUpdateFinished(object sender, SynchronizationUpdateEventArgs e)
        {
            if (e.Type == typeof(T))
            {
                ReloadModel();
            }
        }

        IRepositoryBase<T, TKey> _repository; 
        protected virtual IRepositoryBase<T, TKey> Repository 
        {
            get { return _repository ?? (_repository = Dal.Repository.GetRepository<T, TKey>()); }
        }

        public Exception Exception
        {
            get;
            private set;
        }
        protected void ClearException()
        {
            Exception = null;
        }

        protected void HandleException(Exception exception)
        {
            Exception = exception;
        }

        public override void ReloadModel()
        {

        }

    }
}